/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    FanZhiwen
 * 创建时间:    2023-10-16
 */
package org.jeecg.modules.flow.callback.inbound.order;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.business.doc.business.entity.WmsInReceipt;
import org.jeecg.modules.business.doc.business.entity.WmsInReceiptMaterial;
import org.jeecg.modules.business.doc.business.service.IWmsInReceiptMaterialService;
import org.jeecg.modules.business.doc.business.service.IWmsInReceiptService;
import org.jeecg.modules.business.third.service.IMesStatusService;
import org.jeecg.modules.flow.callback.inbound.mapper.SendMesDataVO;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  入库单提交后,给MES同步卷料批次、鞍座号
 *
 * @author FanZhiwen
 * @version 1.0.0
 * @since 2023-10-16
 */
@Component
@Slf4j
public class InReceiptSendMesDataBeforeBean<T> extends BaseCustomBeanRun<T> {
    @Autowired
    private IWmsInReceiptService iWmsInReceiptService;
    @Autowired
    private IWmsInReceiptMaterialService inReceiptMaterialService;
    @Autowired
    private IMesStatusService iMesStatusService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void actureRun() {
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        String id = baseFlowDto.getInfoId();
        WmsInReceipt wmsInReceipt = iWmsInReceiptService.getById(id);
        if (ObjectUtil.isNotNull(wmsInReceipt)){
            List<WmsInReceiptMaterial> list = inReceiptMaterialService.list(new LambdaQueryWrapper<WmsInReceiptMaterial>()
                    .eq(WmsInReceiptMaterial::getWmsInReceiptId, id)
            );
            if (CollectionUtil.isNotEmpty(list)){
                List<SendMesDataVO> collect = new ArrayList<>();
                //传鞍座号和批次号
                if (Arrays.asList("R07").contains(wmsInReceipt.getDocumentType())){
                    collect = list.stream().map(o->{
                        SendMesDataVO vo=new SendMesDataVO();
                        vo.setBatchId(o.getBatchCode());
                        vo.setPosition(o.getPosition());
                        return vo;
                    }).collect(Collectors.toList());
                }
                //通知mes批次号和鞍座号/批次号和库区
                try {
                    iMesStatusService.warehouse(null,id,3,collect);
                }catch (Exception e){
                    log.error("通知mes批次号和鞍座号出现异常",e);
                }
            }
        }
    }
}
